﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BarberShop.Data;

namespace BarberShop.BLL
{
    public class BarberCommonBLL
    {
        /// <summary>
        /// 获取省份
        /// </summary>
        /// <returns></returns>
        public static IList<Province> GetAllProvinceList()
        {
            using (DataClassesDataContext data = new DataClassesDataContext())
            {
                var list = (from a in data.Province select a).ToList();
                return list;
            }
        }
        /// <summary>
        /// 获取身份实体
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static Province GetProvinceById(int id)
        {
            using (DataClassesDataContext data = new DataClassesDataContext())
            {
                var info = (from a in data.Province where a.ProID == id select a).FirstOrDefault();
                return info;
            }
        }

        /// <summary>
        /// 获取城市
        /// </summary>
        /// <param name="provinceId"></param>
        /// <returns></returns>
        public static IList<City> GetCityListByPID(int  provinceId)
        {
            using (DataClassesDataContext data = new DataClassesDataContext())
            {
                var list = (from a in data.City where a.ProID==provinceId select a ).ToList();
                return list;
            }
        }
        /// <summary>
        /// 获取城市实体
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static City GetCityById(int id, int proID)
        {
            using (DataClassesDataContext data = new DataClassesDataContext())
            {
                var info = (from a in data.City where a.CityID == id && a.ProID == proID select a).FirstOrDefault();
                return info;
            }
        }
        /// <summary>
        /// 获取门店设备数量
        /// </summary>
        /// <param name="id"></param>
        /// <param name="status">-1所有,0下线,1上线</param>
        /// <returns></returns>
        public static int GetDeviceNumByStoreId(int id,int status)
        {
            using (DataClassesDataContext data = new DataClassesDataContext())
            {
                IQueryable<DeviceInfo> list;
                if (status == -1)
                {
                    list = (from a in data.DeviceInfo where a.Stores_ID == id select a);
                }
                else
                {
                    list = (from a in data.DeviceInfo where a.Stores_ID == id&&a.LineStatus==status select a);
                }

                return list.Count() ;
            }
        }

        /// <summary>
        /// 获取品牌设备数量
        /// </summary>
        /// <param name="id"></param>
        /// <param name="status"></param>
        /// <returns></returns>
        public static int GetDeviceNumByBrandId(int id, int status)
        {
            using (DataClassesDataContext data = new DataClassesDataContext())
            {
                var sotrelist = (from a in data.StoresInfo where a.Brand_ID == id select a.ID);
                int num = 0;
                foreach (var item in sotrelist)
                {
                    num+=GetDeviceNumByStoreId(item, status);
                }
                return num;
            }
        }
    }
}
